
import com.ContaContabil;

import com.TipoDeOperacao;
import groovy.sql.Sql
import org.apache.shiro.crypto.hash.Sha256Hash

// ALEX LIMA

class BootStrap {
	def dataSource
    
	def init = { servletContext ->
	
		def user1 = new ShiroUser(username: "admin", passwordHash: new Sha256Hash("teste").toHex())
		user1.addToPermissions("*:*")
		user1.save()
	
		def user2 = new ShiroUser(username: "gerente", passwordHash: new Sha256Hash("123").toHex())
		user2.addToPermissions("gastos:*, vendas:*")
		user2.save()

		// HENRIQUE
			
		def user3 = new ShiroUser(username: "funcionario", passwordHash: new Sha256Hash("123").toHex())
		user3.addToPermissions("gastos:* ")
		user3.addToPermissions("vendas:*")
		user3.save()
		
		ContaContabil contaContabil1 = ContaContabil.findByCodigo(ContaContabil.ATIVO)
		 
		if (contaContabil1 == null){
			def sql = new Sql(dataSource)
			def insert = "INSERT INTO conta_contabil VALUES (1,0,'1','ATIVO','Debito',NULL,1,0)"
			sql.execute(insert)
		}
		
		ContaContabil contaContabil2 = ContaContabil.findByCodigo(ContaContabil.PASSIVO)
		
	   if (contaContabil2 == null){
		   def sql = new Sql(dataSource)
		   def insert = "INSERT INTO conta_contabil VALUES (2,0,'2','PASSIVO','Credito',NULL,1,0)"
		   sql.execute(insert)
	   }
	   ContaContabil contaContabil3 = ContaContabil.findByCodigo(ContaContabil.RECEITA)
	   
	  if (contaContabil3 == null){
		  def sql = new Sql(dataSource)
		  def insert = "INSERT INTO conta_contabil VALUES (3,0,'3','RECEITA','Credito',NULL,1,1)"
		  sql.execute(insert)
	  }
	  
	  ContaContabil contaContabil4 = ContaContabil.findByCodigo(ContaContabil.CUSTOS_DESPESAS)
	  if (contaContabil4 == null){
		  def sql = new Sql(dataSource)
		  def insert = "INSERT INTO conta_contabil VALUES (4,0,'4','CUSTOS_DESPESAS','Debito',NULL,1,1)"
		  sql.execute(insert)
	  }   
	  
	  ContaContabil contaContabil5 = ContaContabil.findByCodigo(ContaContabil.CLIENTE)
	  if (contaContabil5 == null){
		  def sql = new Sql(dataSource)
		  def insert = "INSERT INTO conta_contabil VALUES (5,0,'1-2','CLIENTE','Debito',1,1,0)"
		  sql.execute(insert)
	  }
	  
	  ContaContabil contaContabil6 = ContaContabil.findByCodigo(ContaContabil.CONTA_CORRENTE)
	   if (contaContabil6 == null){
		  def sql = new Sql(dataSource)
		  def insert = "INSERT INTO conta_contabil VALUES (6,0,'1-3','CONTA_CORRENTE','Debito',1,1,0)"
		  sql.execute(insert)
	  }
	 
	    ContaContabil contaContabil7 = ContaContabil.findByCodigo(ContaContabil.FORNECEDOR)
	 
	   if (contaContabil7 == null){
		  def sql = new Sql(dataSource)
		  def insert = "INSERT INTO conta_contabil VALUES (7,0,'2-1','FORNECEDOR','Credito',2,1,0)"
		  sql.execute(insert)
	  }
		
    }
    def destroy = {
    }
}
